package controllers;

import beans.InhoudBean1;
import connectivity.Dbmanager;
import connectivity.QueryManager;
import java.io.*;
import java.util.LinkedList;
import javax.servlet.*;
import javax.servlet.http.*;
import models.Course;
import models.Module;
import tools.Checker;

public class NewModuleController extends HttpServlet {

    private Dbmanager dbManager;
    private QueryManager queryManager;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        dbManager = new Dbmanager();
        dbManager.openConnection();
        int userId, courseId;
        queryManager = new QueryManager(dbManager);
        HttpSession sessie = request.getSession();
        LinkedList<Course> courses;
        if (!request.getParameter("id").isEmpty()) {
            courseId = Integer.parseInt(request.getParameter("id").toString());
            request.setAttribute("courseId", courseId);
        }
        userId = Integer.parseInt(sessie.getAttribute("userId").toString());
        courses = queryManager.getAllTeacherCourses(userId);
        request.setAttribute("courses", courses); 
        dbManager.closeConnection();
        RequestDispatcher dispatcher = request.getRequestDispatcher("/newModule.jsp");
        dispatcher.forward(request, response);

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        dbManager = new Dbmanager();
        dbManager.openConnection();
        queryManager = new QueryManager(dbManager);
        String moduleName = "", moduleDescription = "", address = "";
        boolean isEmpty = false;
        int courseID;
        HttpSession sessie = request.getSession();
        InhoudBean1 inhoud = (InhoudBean1) sessie.getAttribute("inhoud");

        courseID = Integer.parseInt(request.getParameter("course"));

        if (!request.getParameter("moduleName").isEmpty()) {
            moduleName = request.getParameter("moduleName");
            request.setAttribute("moduleName", moduleName);
        } else {
            isEmpty = true;
            request.setAttribute("foutMelding", inhoud.getError1());
        }
        if (!request.getParameter("moduleDescription").isEmpty()) {
            moduleDescription = request.getParameter("moduleDescription");
            request.setAttribute("moduleDescription", moduleDescription);
        } else {
            isEmpty = true;
            request.setAttribute("foutMelding", inhoud.getError2());
        }


        if (!isEmpty) {
            Module module = new Module(moduleName, moduleDescription, courseID);
            queryManager.addModule(module);
            dbManager.closeConnection();
            response.sendRedirect("subscribe_to_course?leraar=true");
        } else {
            LinkedList<Course> courses = queryManager.getAllCourses();
            request.setAttribute("courses", courses);
            address = "/newModule.jsp";
            dbManager.closeConnection();
            RequestDispatcher dispatcher = request.getRequestDispatcher(address);

            dispatcher.forward(request, response);
        }

    }
}
